﻿//==============================================================================
// File: TypeHelper.cs
// Created: 2010-05-10
// Author: Piotr Włodek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with WpfRcp. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;

namespace WpfRcp.PropertyModel.Internal.Common
{
    internal static class TypeHelper
    {
        private static System.Reflection.PropertyInfo GetPropertyInternal(LambdaExpression propertyAccessor)
        {
            MemberExpression memberExpression;

            if (propertyAccessor.Body is UnaryExpression)
            {
                var ue = (UnaryExpression)propertyAccessor.Body;
                memberExpression = (MemberExpression)ue.Operand;
            }
            else
            {
                memberExpression = (MemberExpression)propertyAccessor.Body;
            }

            return (System.Reflection.PropertyInfo)(memberExpression).Member;
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters",
            Justification = "Adds static compile-time checking to expression")]
        public static Type GetPropertyType<TObject>(Expression<Func<TObject, object>> propertyAccessor)
        {
            return GetPropertyInternal(propertyAccessor).PropertyType;
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters",
            Justification = "Adds static compile-time checking to expression")]
        public static string GetPropertyName<TObject>(Expression<Func<TObject, object>> propertyAccessor)
        {
            return GetPropertyInternal(propertyAccessor).Name;
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters",
            Justification = "Adds static compile-time checking to expression")]
        public static string GetPropertyName<TObject, T>(Expression<Func<TObject, T>> propertyAccessor)
        {
            return GetPropertyInternal(propertyAccessor).Name;
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters",
            Justification = "Adds static compile-time checking to expression")]
        public static string GetPropertyName<T>(Expression<Func<T>> propertyAccessor)
        {
            return GetPropertyInternal(propertyAccessor).Name;
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters",
            Justification = "Adds static compile-time checking to expression")]
        public static System.Reflection.PropertyInfo GetProperty<TObject>(Expression<Func<TObject, object>> propertyAccessor)
        {
            return GetPropertyInternal(propertyAccessor);
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters",
            Justification = "Adds static compile-time checking to expression")]
        public static System.Reflection.PropertyInfo GetProperty<TObject, T>(Expression<Func<TObject, T>> propertyAccessor)
        {
            return GetPropertyInternal(propertyAccessor);
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters",
            Justification = "Adds static compile-time checking to expression")]
        public static System.Reflection.PropertyInfo GetProperty<T>(Expression<Func<T>> propertyAccessor)
        {
            return GetPropertyInternal(propertyAccessor);
        }

        public static Type GetMostDerievedType(IEnumerable<Type> types)
        {
            if (types.Count() == 0)
            {
                return null;
            }

            if (types.Count() == 1)
            {
                return types.Single();
            }

            var typeList = new List<Type>(types);
            for (int i = 0; i < typeList.Count; i++)
            {
                var mostDerieved = true;
                for (int j = 0; j < typeList.Count; j++)
                {
                    if (typeList[i] != typeList[j])
                    {
                        if (typeList[j].IsAssignableFrom(typeList[i]) == false)
                        {
                            mostDerieved = false;
                            break;
                        }
                    }
                }

                if (mostDerieved)
                {
                    return typeList[i];
                }
            }

            return null;
        }
    }
}